package com.feng.gaoerfu.view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.feng.gaoerfu.R;

import java.util.List;

/**
 * @time 2017/8/7 0007 15:11
 */

public class MenuDialog extends Dialog {
    public MenuDialog(@NonNull Context context) {
        super(context);
    }

    public MenuDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected MenuDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private final Context context;
        private List<String> list;
        private BaseQuickAdapter<String> mAdapter;

        public Builder(Context context) {
            this.context = context;
        }
        public Builder setList(List<String> list){
            this.list = list;
            return this;
        }
        public MenuDialog build(){
            final MenuDialog dialog = new MenuDialog(context, R.style.Dialog);
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View contentView = inflater.inflate(R.layout.dialog_layout, null);
            RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.dialog_recycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));

            mAdapter = new BaseQuickAdapter<String>(R.layout.item_dialog, list) {
                @Override
                protected void convert(BaseViewHolder baseViewHolder, String s) {
                    baseViewHolder.setText(R.id.item_tv, s);
                }
            };
            recyclerView.setAdapter(mAdapter);
            mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    Toast.makeText(context,list.get(i),Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            });

            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(Gravity.BOTTOM);
            dialogWindow.setWindowAnimations(R.style.dialogstyle); // 添加动画
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0); //消除边距

            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            dialogWindow.setAttributes(lp);

            dialog.addContentView(contentView, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return dialog;
        }
    }
}
